(function($){
$.ajaxTransport("+*", function(s){    
  var xhr;
  if (s.XHR)
    return {
      send: function(headers, complete){   
        xhr = s.xhr();
        xhr.open( s.type, s.url, s.async );
        delete headers["Content-Type"];
        headers[ "X-Requested-With" ] = "XMLHttpRequest";
    
        for ( i in headers ) {
         xhr.setRequestHeader( i, headers[ i ] );
        }
  	
      	var callback = function(e){
      	  var responses = {xml: xhr.responseXML, text: xhr.responseText};
      	  complete( xhr.status, xhr.statusText, responses, xhr.getAllResponseHeaders() );
      	};
  	
      	xhr.addEventListener("load", callback);
      	xhr.addEventListener("error", callback);

        xhr.send(s.data);
        
      },
  
      abort: function(){
        if (xhr) xhr.abort();
      }
  };
});

var defaultSettings = {
	processData: false,
	contentType: false,
	type:        "POST",
	XHR:      true
};

function filename(filePath) {
	return filePath.replace(/.*(\/|\\)/, "");
}

function TranslateURL(src){
	//转换img的路径
	return src.substring(1,src.length);
}

var MultiUpload = MultiUpload || {};

MultiUpload = {
	init:function(){
		this._initUpload("input#uploadfile");
		this.bindEvent();
	},
	_events:{
		
	},
	
	checksize:function(file){
		//未写完
	},
	
	bindEvent:function(){
		
	},
	
	_initUpload:function(element){
		var self = this;
		$(element).ajaxPhotoInfo({
			onChange: self.Change,
			onComplete: self.Complete
		});
	},
	
	Error:function(){
		
	},
	
	Change:function(e){
		var element	= $(e.target),
            file	= filename(element.val()),
            form = $("#upload");
        //判断大小？
        MultiUpload.checksize(file);
		form.submit();
		//如何提示正在加载？？
		//$("#Upload-img").append('');   //提示正在加载
	},
	Complete:function(e){
		//取消正在加载？？
		var iframe  = $("iframe[name=result]"),
            doc = iframe[0].contentDocument ? iframe[0].contentDocument : window.frames[iframe[0].name].document,
            response = doc.body.innerHTML;
        if(response){
        	var json = eval("(" + response + ")");
        	var target = $(".Upload-img #img-area");
			if(json.success == "true"){
				for(var e in json.data){
					var text ="<div pid = '"+json.data[e].uid+"' class = 'img-info'>";
					text += " <div><img src='"+ TranslateURL(json.data[e].url)+"' ></div>";
					text +="<div>";
					text += "<p title='点击修改' name = 'descrip' class = 'img-discrip'>照片描述:</p>";
					text +="<div class = 'textarea-div'> ";
					text +="<textarea type='text' wrap='virtual' maxlength='500'></textarea> ";
					text +="<input type='button' value='保存' class = 'button-save'/>";
					text +="<input type='button' value='取消' class = 'button-cancel'/>";
					text +="</div></div></div>";
					//插入到Html中
					//target.html(text+target.html());
					target.append(text);
					//MultiUpload.init();
					BindImgEvent();
				};
			}
			else{
				//显示加载失败
				MessageShow("加载图片失败，请重试");
			}
			
        }
	},
	
	UpdataImg:function(e){
		
	}
	
	
};



$.fn.ajaxPhotoInfo = function(options){
	var el = this;
	var defaults = {
		onChange:function(e){},
		onComplete:function(e){},
		onError:function(e){}
	};
	var defaultSettings = $.extend({}, defaults, options);
	this.each(function(){
		var $this = $(this);
		if($this.is("input") && $this.attr("type") === "file"){
			$this.bind("change",defaultSettings.onChange);
		}
	});
	$("iframe[name=result]").bind('load', defaultSettings.onComplete);
	
}

MultiUpload.init();

})(jQuery)
